#!/bin/sh
exec java -jar jaseries.jar -nocompdaemon -usejavacp -savecompiled "$0" "$@"
!#

import io.Source
import fr.janalyse.series._

// Get nasdaq codes
val codes = 
  Source.fromURL("http://www.nasdaqtrader.com/dynamic/SymDir/nasdaqlisted.txt")
    .getLines
    .map(_.split("[|]",2).head)
    .toList.tail.init //.take(10)

// Get nasdaq stocks history
val stocks =
  codes.flatMap(code =>
    try {
    	Some(code -> CSV2Series.fromURL("http://ichart.finance.yahoo.com/table.csv?s="+code))
    } catch {
      case x:java.io.FileNotFoundException => 
        println("Could'nt find %s stock data".format(code))
        None
    }
  )

// get close series
val closes = stocks flatMap { case (code, allSeries) => 
  allSeries.get("Close").map(_.rename(code+" Close"))
}

val top10incr =
  closes
    .sortBy( _.stat.linearApproximation.slope)
    .take(20)
    
// print highest slope top20, but remember that all history data is taken into
// account, so the last value may not be quiter higher than the first one.
for(stock <- top10incr) {
  println("%s : %f -> %f".format(stock.name, stock.head.value, stock.last.value))
}

